package cn.loioi.web.components.beetl.fun;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringUtil extends org.beetl.ext.fn.StringUtil {

	@Override
	public String[] split(String str, String key) {
		if (str != null && !"".equals(str)) {
			if (key == null || key.length() == 0) {
				key = ",";
			}
			return str.split(key);
		}
		String[] re = {};
		return re;
	}

	public String[] split(String str) {
		return split(str, ",");
	}

	@Override
	public String trim(String str) {
		if (str == null) {
			str = "";
		}
		return str.trim();
	}

	@Override
	public boolean startWith(String str, String key) {
		if (str == null) {
			return false;
		}
		return str.startsWith(key);
	}

	@Override
	public boolean endWith(String str, String key) {
		if (str == null) {
			return false;
		}
		return str.endsWith(key);
	}

	@Override
	public int length(String str) {
		if (str == null) {
			return -1;
		}
		return str.length();
	}

	@Override
	public String subStringTo(String str, int start, int end) {
		if (str == null) {
			return "";
		}
		return str.substring(start, end);
	}

	@Override
	public String subString(String str, int start) {
		if (str == null) {
			return "";
		}
		return str.substring(start);
	}

	@Override

	public boolean contain(String str, String key) {
		if (str == null) {
			return false;
		}
		return str.indexOf(key) != -1;
	}

	@Override
	public String toUpperCase(String str) {
		if (str == null) {
			return "";
		}
		return str.toUpperCase();
	}

	@Override
	public String toLowerCase(String str) {
		if (str == null) {
			return "";
		}
		return str.toLowerCase();
	}

	@Override
	public String replace(String str, String oldValue, String newValue) {
		if (str == null) {
			return "";
		}
		return str.replace(oldValue, newValue);
	}

	@Override
	public String format(String str, Object[] args) {
		if (str == null) {
			return "";
		}
		String result = MessageFormat.format(str, args);
		return result;
	}

	@Override
	public String formatDate(Date o, String args) {
		if (o == null) {
			return "";
		}
		SimpleDateFormat sd = new SimpleDateFormat(args);
		return sd.format(o);
	}

	@Override
	public int index(String str, String search) {
		if (str == null) {
			return -1;
		}
		return str.indexOf(search);
	}

	@Override
	public int lastIndex(String str, String search) {
		if (str == null) {
			return -1;
		}
		return str.lastIndexOf(search);
	}
}
